package com.Map_Practice;

public class StudentController {
	static public void addStudent() {

		System.out.println("请输入学生学号：");
		int id = Integer.valueOf(Input.sc.nextLine());
		System.out.println("请输入学生姓名：");
		String name = String.valueOf(Input.sc.nextLine());
		System.out.println("请输入学生成绩：");
		double score = Double.valueOf(Input.sc.nextLine());

		Result result = MapDemo.add(name, id, score);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("是否继续添加（ y 继续添加）");
			String in = String.valueOf(Input.sc.nextLine());
			if ("y".equals(in))
				addStudent();
		}
		System.out.println("=====  结束新增学员  =====");
	}

	public static void deleteStudent() {
		System.out.println("请输入要删除学生的学号：");
		int id = Integer.valueOf(Input.sc.nextLine());

		Result result = MapDemo.Delete(id);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("是否继续（ y 继续）");
			String in = String.valueOf(Input.sc.nextLine());
			if ("y".equals(in))
				deleteStudent();
		}
		System.out.println("=====  结束删除学员  =====");
	}

	static private int checkIdValid() {
		int id = Integer.valueOf(Input.sc.nextLine());
		Student student = MapDemo.find(id);
		if (student == null) {
			System.out.println("该学员不存在，请重新输入");
			checkIdValid();
		}
		return id;
	}

	public static void editStudent() {
		System.out.println("请输入要修改学生的学号：");
		int id = checkIdValid();
		System.out.println("请修改后学生的姓名（空为不修改）：");
		String name = Input.sc.nextLine();
		System.out.println("请修改后学生的成绩（空为0）：");
		String scoreStr = Input.sc.nextLine();

		Student student = new Student();
		student.setId(id);
		student.setName(name);

		double score = 0d;
		if (scoreStr != null && !"".equals(scoreStr)) {
			score = Double.valueOf(scoreStr);
		}
		student.setScore(score);
		Result result = MapDemo.Edit(id, score, name);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("是否继续（ y 继续）");
			String in = Input.sc.nextLine();
			if ("y".equals(in)) {
				editStudent();
			}
		}
		System.out.println("=====  结束修改学员信息  =====");
	}

	public static void findStudent() {
		System.out.println("请输入学生的学号：");
		int id = Integer.valueOf(Input.sc.nextLine());
		if (MapDemo.find(id) != null) {
			System.out.println(MapDemo.find(id));
		} else {
			System.out.println("无该学生！");
		}
	}
	
	
	public static void Load() {
		try {
			System.out.println("========加载学员开始========");
			MapDemo.deserialize();
			System.out.println("========加载学员完毕========");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void exit() {
		try {
			System.out.println("========写入学员信息开始========");
			MapDemo.serialize();
			System.out.println("========写入成功/退出程序========");
			System.exit(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
